# FROM debian:stable-slim@sha256:e81f4f26f78026296fdde4e512c77f1d9b90433b4a86fb7cdb70a4cdb5ba73a2
# FROM debian:stable
# FROM debian:buster-backports
# FROM debian:stretch
FROM debian:buster-slim@sha256:240f770008bdc538fecc8d3fa7a32a533eac55c14cbc56a9a8a6f7d741b47e33



# NOTE: specify:
# --build-arg PASSWORD_HASH="$(openssl passwd -6 -salt 'YOURSALT' --verbose 'YOURPASSWORD')"
# when building the image

###############
# PREPARATION #
###############

ARG DEBIAN_FRONTEND="noninteractive"
# change the codename if switching to another version
ARG DEBIAN_CODENAME="buster"

##############
# BUILD ARGS #
##############

ARG PASSWORD_HASH

################
# SYSTEM FIXES #
################

# To avoid getting the error:

# invoke-rc.d: could not determine current runlevel
# invoke-rc.d: policy-rc.d denied execution of reload.

# We can define, that policy-rc.d returns 0, which stands for allow starting of a service.

# Details:
# https://jpetazzo.github.io/2013/10/06/policy-rc-d-do-not-start-services-automatically/
# and https://stackoverflow.com/a/48782486

RUN printf '#!/bin/sh\nexit 0' > /usr/sbin/policy-rc.d

###################
# SYSTEM PACKAGES #
###################

USER root
RUN apt-get update

# PACKAGES:
# ca-certificates: required to make secure downloads
# apt-transport-https: is used in tutorials to download apt packages via HTTPS
# apt-file: allows searching for packages by file namex
# systemd-services: is required for the hostnamectl command to be available
# gnupg: required for dearmoring a gpg key

RUN apt-get install \
    --yes \
    --no-install-recommends \
    wget \
    ca-certificates \
    apt-transport-https \
    apt-file \
    systemd \
    gnupg

#########
# USERS #
#########

USER root
RUN useradd \
    --create-home \
    --user-group \
    --password "${PASSWORD_HASH}" \
    --shell /bin/bash \
    jitsi
USER jitsi

###########
# OPENJDK #
###########

USER root

# BUGFIX: /usr/share/man/man1 needs to exist before installing Open JDK.
# Details: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=863199#23
RUN mkdir --parents /usr/share/man/man1

ARG OPENJDK_GPG_KEY_DOWNLOAD_URL="https://adoptopenjdk.jfrog.io/adoptopenjdk/api/gpg/key/public"
ARG OPENJDK_GPG_KEY_FILE_NAME="openjdk.gpg.key"
RUN wget --quiet --output-document "${OPENJDK_GPG_KEY_FILE_NAME}" "${OPENJDK_GPG_KEY_DOWNLOAD_URL}"
RUN apt-key add "${OPENJDK_GPG_KEY_FILE_NAME}"
RUN echo 'deb https://adoptopenjdk.jfrog.io/adoptopenjdk/deb buster main' | tee /etc/apt/sources.list.d/adoptopenjdk.list > /dev/null
RUN apt-get update
RUN apt-get install \
    --yes \
    --no-install-recommends \
    adoptopenjdk-8-hotspot \
    ca-certificates-java

#########
# NGINX #
#########

USER root
RUN apt-get install \
    --yes \
    --no-install-recommends \
    nginx

#########
# JITSI #
#########

USER root

# configure hostname
ARG HOSTNAME="localhost.com"
ARG HOST_IP_ADDRESS="127.0.0.1"
RUN apt-file search hostnamectl
RUN hostnamectl set-hostname "${HOSTNAME}"
RUN printf "${HOST_IP_ADDRESS}        ${HOSTNAME}" >> /etc/hosts

# PROBLEM: Cannot set hostname:

# System has not been booted with systemd as init system (PID 1). Can't operate.
# Failed to create bus connection: Host is down
# The command '/bin/sh -c hostnamectl set-hostname "${HOSTNAME}"' returned a non-zero code: 1

# add jitsi gpg key
ARG JITSI_KEY_DOWNLOAD_URL="https://download.jitsi.org/jitsi-key.gpg.key"
ARG JITSI_KEY_FILE_NAME="jitsi-key.gpg.key"
RUN wget --quiet --output-document "${JITSI_KEY_FILE_NAME}" "${JITSI_KEY_DOWNLOAD_URL}"
RUN apt-key add "${JITSI_KEY_FILE_NAME}"

# add jitsi repository
RUN echo 'deb https://download.jitsi.org stable/' | tee /etc/apt/sources.list.d/jitsi-stable.list > /dev/null

# update repositories
RUN apt-get update  # --option 'Dir::Etc::sourcelist=/etc/apt/sources.list.d/jitsi-stable.list'

# install jitsi dependencies and jitsi-meet
RUN apt-get install \
    --yes \
    --no-install-recommends \
    jitsi-meet

# PROBLEM: NGINX probably needs to be already running for Jitsi to install successfully.

###############
# APT CLEANUP #
###############

USER root
RUN rm /etc/apt/sources.list.d/stretch.list
RUN apt-get clean
RUN rm --recursive --force /var/lib/apt/lists/*

########
# TODO #
########

# sudo ufw allow 80/tcp
# sudo ufw allow 443/tcp
# sudo ufw allow 10000/udp
# sudo ufw allow 22/tcp
# sudo ufw allow 3478/udp
# sudo ufw allow 5349/tcp
# sudo ufw enable

# sudo ufw status verbose

# or use iptables for that

################
# FINALIZATION #
################

USER jitsi
# start jitsi?
# entrypoint?
